package com.pshcp.patient.tool;

import com.external.pay.domain.IcbcNotifyBizContentDTO;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.PayModeEnum;
import com.pshcp.common.enums.RegPayStatusEnum;
import com.pshcp.domain.model.PayInfo;
import com.pshcp.patient.domain.paynotify.PayNotifyDTO;
import com.wechat.pay.java.service.payments.model.Transaction;

import java.math.BigDecimal;

public class PayNotifyTools {

    /**
     * build ICBC notify
     * @param bizContentDTO
     * @return
     */
    public static PayNotifyDTO buildIcbcPayNotify(Integer payBusiness, IcbcNotifyBizContentDTO bizContentDTO) {
        PayNotifyDTO dto = new PayNotifyDTO();
        dto.setBizId(Long.parseLong(bizContentDTO.getOut_trade_no()));
        dto.setPayBusiness(payBusiness);
        dto.setPayMode(getPayModeByIcbc(bizContentDTO.getPay_type()));
        dto.setAmount(new BigDecimal(bizContentDTO.getPayment_amt()).divide(new BigDecimal(100)));
        dto.setPayStatus(RegPayStatusEnum.PAY_OK.getCode());
        dto.setIcbcOrderId(bizContentDTO.getOrder_id());
        dto.setThirdNo(bizContentDTO.getThird_trade_no());
        return dto;
    }

    /**
     * @param payType 9-微信；10-支付宝；13-云闪付
     * @return
     */
    public static Integer getPayModeByIcbc(String payType) {
        if("9".equals(payType)){
            return PayModeEnum.WECHAT_PAY.getCode();
        }
        if("10".equals(payType)){
            return PayModeEnum.ALI_PAY.getCode();
        }
        if("13".equals(payType)){
            return PayModeEnum.UNION_PAY.getCode();
        }
        return null;
    }


    /**
     * build wechat notify
     * @param transaction
     * @return
     */
    public static PayNotifyDTO buildWechatPayNotify(Integer payBusiness, Transaction transaction) {
        PayNotifyDTO dto = new PayNotifyDTO();
        dto.setBizId(Long.parseLong(transaction.getOutTradeNo()));
        dto.setPayBusiness(payBusiness);
        dto.setPayMode(PayModeEnum.WECHAT_PAY.getCode());
        dto.setAmount(new BigDecimal(transaction.getAmount().getTotal()).divide(new BigDecimal(100)));
        dto.setPayStatus(RegPayStatusEnum.PAY_OK.getCode());
        dto.setThirdNo(transaction.getTransactionId());
        return dto;
    }

    public static PayInfo buildPayInfo(PayNotifyDTO payNotifyDTO){
        PayInfo payInfo = CommonTool.copyProperties(payNotifyDTO, PayInfo.class);

        return payInfo;
    }

}
